package com.joey.cloud.flowable.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.joey.cloud.flowable.acthi.entity.ActHiComment;
import com.joey.cloud.flowable.acthi.mapper.ActHiCommentMapper;
import com.joey.cloud.flowable.dto.TaskEventMsgDto;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.HistoryService;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 流程事件业务
 */
@Service
@Slf4j
public class FlowEventService {
    @Resource
    RuntimeService runtimeService;
    @Resource
    HistoryService historyService;
    @Resource
    RepositoryService repositoryService;
    @Resource
    ActHiCommentMapper actHiCommentMapper;

    /**
     * 发送消息
     * @param dto
     */
    public void sendEventMsg(TaskEventMsgDto dto) {
        log.info("发送流程事件消息",dto);
            //TODO 发送消息
    }

    /**
     * 构建消息
     * @param eventType 事件类型
     * @param taskId    任务id
     * @param processInstanceId 流程实例id
     * @param currentTaskStep 任务环节key
     */
    public void buildEventMsg(String eventType,String processInstanceId,String taskId,String currentTaskStep) {
        log.info("构建流程事件消息",eventType);
        TaskEventMsgDto dto = new TaskEventMsgDto();
        dto.setTaskId(taskId);
        dto.setProcessInstanceId(processInstanceId);
        dto.setCurrentStep(currentTaskStep);
        dto.setEventType(eventType);
        ProcessInstance process = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
        if(process!=null){
            dto.setProcessDefinitionId(process.getProcessDefinitionId());
            dto.setBusinessKey(process.getBusinessKey());
            dto.setParam(process.getProcessVariables());
            dto.setUserId(process.getStartUserId());
        }else {
            HistoricProcessInstance hisProcess = historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
            if(hisProcess!=null){
                dto.setProcessDefinitionId(hisProcess.getProcessDefinitionId());
                dto.setBusinessKey(hisProcess.getBusinessKey());
                dto.setParam(hisProcess.getProcessVariables());
                dto.setUserId(hisProcess.getStartUserId());
            }
        }
        ProcessDefinition definition = repositoryService.createProcessDefinitionQuery().processDefinitionId(dto.getProcessDefinitionId()).singleResult();
        if(definition!=null){
            dto.setSolKey(definition.getKey());
        }
        QueryWrapper qw = new QueryWrapper();
        qw.select("user_id_");
        qw.eq("PROC_INST_ID_",processInstanceId);
        qw.isNotNull("TASK_ID_");
        qw.orderByDesc("TIME_");
        qw.last(" limit 1");
        ActHiComment one = actHiCommentMapper.selectOne(qw);
        if(one != null){
            dto.setUserId(one.getUserId());
        }
        this.sendEventMsg(dto);

    }


}
